package com.list.data.structure;

import java.util.ArrayList;
import java.util.List;

public class ShortestPathResult extends ArrayList<ShortestPath> {
	
	private int size;
	
	public void sort(){
		this.sort();
	}
	@Override
	public boolean add(ShortestPath path) {
		size = this.size();
		int index = binarySearch(path.getPathScore());
		super.add(index, path);
		return this.size() > size;
	}
	
    private int binarySearch(double value) {
        int low = 0;
        int high = this.size() - 1;

        while (low <= high) {
            int mid = (low + high) / 2;
            Double midVal = this.get(mid).getPathScore();
            int cmp = midVal.compareTo(value);
            
            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid;
        }
        return low;
    }
    
    public boolean insert(ShortestPath path) {
    	return super.add(path);
    }
    
    @Override
    public String toString() {
    	
    	StringBuffer sb = new StringBuffer();
    	for (ShortestPath path : this) {
			sb.append(path.toString() + "\n");
		}
    	
//    	sb.append("\n");
    	return sb.toString();
    }
}
